{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义一个节点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Node:\n",
    "    def __init__(self,value,node = 0):\n",
    "        self.value = value\n",
    "        self.next = node"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 定义一个LinkedList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LinkedList:\n",
    "    def __init__(self,value=0, *args):\n",
    "        self.lenth = 0\n",
    "        # 创建表头head\n",
    "        self.head = 0 if value == 0 else Node(value)\n",
    "        # 如果初始化实例时传入多个参数，循环加入链表\n",
    "        p = self.head\n",
    "        for i in [*args]:\n",
    "            node = Node(i)\n",
    "            p.next = node\n",
    "            p = p.next\n",
    "\n",
    "    def printLinkedList(self):\n",
    "        self.p = self.head\n",
    "        while self.p:\n",
    "            print(self.p.value)\n",
    "            if not self.p.next:\n",
    "                return self.p\n",
    "            self.p = self.p.next\n",
    "\n",
    "#在此添加代码append,insert\n",
    "    def append(self,value):\n",
    "        new_node=Node(value)\n",
    "        cur=self.head\n",
    "        while cur.next!=0:\n",
    "            cur=cur.next\n",
    "        cur.next=new_node\n",
    "\n",
    "    def insert(self,value):\n",
    "        self.head = Node(value,self.head)\n",
    "\n",
    "    def insert_anywhere(self,index,value):\n",
    "        if self.head is None or index <= 0:\n",
    "            self.head = Node(value,self.head)\n",
    "        else:\n",
    "            temp = self.head\n",
    "            while index > 1 and temp.next != None:\n",
    "                temp = temp.next\n",
    "                index = index-1\n",
    "            temp.next = Node(value,temp.next)\n",
    "        self.lenth = self.lenth+1\n",
    "    def searchIndex(self,value):\n",
    "        temp = self.head\n",
    "        index = 0\n",
    "        while temp != None and value != temp.value:\n",
    "            index = index+1\n",
    "            temp = temp.next\n",
    "        return index\n",
    "\n",
    "\n",
    "    def delete(self,value):\n",
    "        index = self.searchIndex(value)\n",
    "        if index <=0 or self.head.next is None:\n",
    "            removeItem = self.head.value\n",
    "            self.head = self.head.next\n",
    "        else:\n",
    "            temp = self.head\n",
    "            while index>1 and temp.next.next != None:\n",
    "                temp = temp.next\n",
    "                index = index-1\n",
    "            removeItem = temp.next.value\n",
    "            temp.next = temp.next.next\n",
    "        self.lenth = self.lenth -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = LinkedList(7,5,3,4,1,2,8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "5\n",
      "3\n",
      "4\n",
      "1\n",
      "2\n",
      "8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<__main__.Node at 0x146883b7148>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.printLinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "7\n",
      "5\n",
      "3\n",
      "4\n",
      "1\n",
      "2\n",
      "8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<__main__.Node at 0x146883b7148>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.insert(9)\n",
    "l.printLinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "7\n",
      "5\n",
      "3\n",
      "1\n",
      "2\n",
      "8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<__main__.Node at 0x146883b7148>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l.delete(4)\n",
    "l.printLinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
